# # test.py
# import time

# import requests
# import base64

# # 测试用基础配置
# URL = "http://127.0.0.1:5000/api/ocr/recognize"  # 确保服务已运行
# TEST_IMAGE_PATH = "/root/autodl-tmp/test.png"


# def test_ocr_api():
#     # 1. 读取测试图片并编码为base64
#     with open(TEST_IMAGE_PATH, "rb") as f:
#         start_time = time.time()
#         img_base64 = base64.b64encode(f.read()).decode("utf-8")

#     # 2. 构造请求数据
#     payload = {"img_base64": img_base64}

#     try:
#         # 3. 发送POST请求
#         response = requests.post(URL, json=payload)

#         # 4. 检查响应状态码
#         if response.status_code == 200:
#             print("✅ 测试成功！响应数据：", response.json())
#             end_time = time.time()
#             print(f"✅ 测试成功！响应时间：{end_time - start_time}秒")
#         else:
#             print(f"❌ 测试失败，状态码：{response.status_code}，响应内容：{response.text}")

#     except Exception as e:
#         print(f"⚠️  请求异常：{str(e)}")


# if __name__ == "__main__":
#     test_ocr_api()




# test.py
import time
import json  # 新增导入
import requests
import base64

URL = "http://127.0.0.1:5000/api/ocr/recognize"
TEST_IMAGE_PATH = "/root/autodl-tmp/ysp-2025-04-27_14-58-05.png"

def test_ocr_api():
    with open(TEST_IMAGE_PATH, "rb") as f:
        start_time = time.time()
        img_base64 = base64.b64encode(f.read()).decode("utf-8")

    payload = {"img_base64": img_base64}

    try:
        response = requests.post(URL, json=payload)
        if response.status_code == 200:
            print("✅ 测试成功！响应数据：", response.json())
            end_time = time.time()
            print(f"✅ 测试成功！响应时间：{end_time - start_time}秒")
            
            # 保存为 JSON 文件
            with open("/root/autodl-tmp/response.json", "w") as json_file:
                json.dump(response.json(), json_file, indent=4)  # indent 用于美化格式
        else:
            print(f"❌ 测试失败，状态码：{response.status_code}，响应内容：{response.text}")

    except Exception as e:
        print(f"⚠️  请求异常：{str(e)}")

if __name__ == "__main__":
    test_ocr_api()